<?php
/*********************************
 * DECLARE SYSTEM CONFIGURATIONS *
 ********************************/
$time_start = microtime(true);

if( ! ini_get('date.timezone') )
{
   date_default_timezone_set('GMT');
}

// Define separators
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
defined('PS') || define('PS', PATH_SEPARATOR);

// Define path to directories
defined('ROOT_DIR') || define('ROOT_DIR', str_replace(DS, '/', dirname(dirname(__FILE__)))); 
defined('APPLICATION_DIR') || define('APPLICATION_DIR', ROOT_DIR.'/application');
defined('DATA_DIR') || define('DATA_DIR', ROOT_DIR.'/data');
defined('LIBRARIES_DIR') || define('LIBRARIES_DIR', ROOT_DIR.'/libraries');
defined('PUBLIC_DIR') || define('PUBLIC_DIR', ROOT_DIR.'/public');

// Define domain name and protocols
defined('DOMAIN') || define('DOMAIN', $_SERVER['HTTP_HOST']);
defined('BASE_URL') || define('BASE_URL', 'http://'.DOMAIN);
defined('BASE_URL_SECURE') || define('BASE_URL_SECURE', 'https://'.DOMAIN);

// Ensure libraries/ is on include_path
set_include_path(implode(PS, array(realpath(LIBRARIES_DIR), get_include_path())));

//Include common libs
include_once('Utils.php');

/* Maintaince */
//maintenance();

/***********************
 * RUN WEB APPLICATION *
 ***********************/
 //Include global constants
include_once(APPLICATION_DIR.'/config/defines.php');
 
// change the following paths if necessary
$yii=LIBRARIES_DIR.'/yii/yii.php';
$config=APPLICATION_DIR.'/config/main.php';

// remove the following lines when in production mode
defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);
Yii::createWebApplication($config)->run();

//debug
$time_end = microtime(true);
$time = $time_end - $time_start;

if(isset($_GET['debug']) && $_GET['debug'] == true)
	echo '<div style="display:block;top:0;left:0;z-index:100000;color:red;clear:both;width:100%;text-align:center;">Execution time: '.round($time, 3).'ms --- Memory usage: '.round(memory_get_usage()/1024/1024, 3).'mb --- Memory peak usage: '.round(memory_get_peak_usage(true)/1024/1024, 3).'mb</div>';	

function o($var){
	echo '<pre>'; var_dump($var); die;
}

function maintenance($min = 300){
	header("HTTP/1.1 503 Service Temporarily Unavailable");
	header("Status: 503 Service Temporarily Unavailable");
	header("Retry-After: {$min}");
	echo    "<!doctype html>
			 <html>
				<head>
					<title>Site is down for maintenence - [".DOMAIN."]</title>
					<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
					<meta http-equiv=\"refresh\" content=\"{$min}\" />
					<style>
						body { text-align: center; padding-top: 150px; }
						h1 { font-size: 50px; }
						body { font: 20px Helvetica, sans-serif; color: #333; }
						article { display: block; text-align: left; width: 650px; margin: 0 auto; }
						a { color: #dc8100; text-decoration: none; }
						a:hover { color: #333; text-decoration: none; }
					</style>
				</head>
				<body>
					<article>
						<h1>Site is temporary unavailable.</h1>
						<p>We are currently performing scheduled maintenance. Site will back soon.</p>
						<p>We apologize for any inconvenience.</p>
						<p>&mdash; <a href=\"".BASE_URL."\">[".DOMAIN."]</a></p>
					</article>
				</body>
			</html>";
	die;
}
